package com.qdlc.p2p.biz.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.qdlc.p2p.biz.service.AdditionalMovieTicketService;
import com.qdlc.p2p.common.util.CollectionUtils;
import com.qdlc.p2p.common.util.StringUtil;
import com.qdlc.p2p.dal.common.model.jpa.OrderFilter;
import com.qdlc.p2p.dal.common.model.jpa.PageDataList;
import com.qdlc.p2p.dal.common.model.jpa.QueryParam;
import com.qdlc.p2p.dal.common.model.jpa.SearchFilter;
import com.qdlc.p2p.dal.dao.AdditionalMovieTicketDao;
import com.qdlc.p2p.dal.dto.AdditionalMovieTicket;
import com.qdlc.p2p.dal.model.AdditionalMovieTicketModel;

/**
 *
 * @author wuaj
 * @version 1.1.0.0
 * @date 2016年03月17日
 */
@Service("additionalMovieTicketService")
public class AdditionalMovieTicketServiceImpl implements AdditionalMovieTicketService {

    @Resource
    private AdditionalMovieTicketDao additionalMovieTicketDao;

    @Override
    public AdditionalMovieTicket save(AdditionalMovieTicket movieTicket) {
        return additionalMovieTicketDao.save(movieTicket);
    }

    @Override
    public AdditionalMovieTicket userTicketDetail(String code, long userId) {
        QueryParam param = QueryParam.getInstance();
        param.addParam("user.userId", userId);
        param.addParam("exchangeCode", code);
        List<AdditionalMovieTicket> list = additionalMovieTicketDao.findByCriteria(param);
        return CollectionUtils.isNotNullOrEmpty(list) ? list.get(0) : new AdditionalMovieTicket();
    }

    @Override
    public PageDataList<AdditionalMovieTicketModel> list(AdditionalMovieTicketModel model) {
        QueryParam param = QueryParam.getInstance().addPage(model.getPage(), model.getRows());
        param.addOrder(OrderFilter.OrderType.DESC, "id");
        if (StringUtil.isNotBlank(model.getSearchName()))
            param.addParam("user.userName", SearchFilter.Operators.LIKE, model.getSearchName().trim());
        PageDataList<AdditionalMovieTicket> pageList = additionalMovieTicketDao.findAllPageList(param);
        PageDataList<AdditionalMovieTicketModel> pageList_ = new PageDataList<AdditionalMovieTicketModel>();
        List<AdditionalMovieTicketModel> list = new ArrayList<AdditionalMovieTicketModel>();
        if (!pageList.getList().isEmpty() || pageList.getList().size() > 0) {
            for (AdditionalMovieTicket ticket : pageList.getList()) {
                AdditionalMovieTicketModel additionalMovieTicketModel = AdditionalMovieTicketModel.instance(ticket);
                additionalMovieTicketModel.setUserName(ticket.getUser().getUserName());
                list.add(additionalMovieTicketModel);
            }
        }
        pageList_.setPage(pageList.getPage());
        pageList_.setList(list);
        return pageList_;
    }
}
